package com.hyt.it.ogt.ykcj.framework.aspectj;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.hyt.it.ogt.ykcj.common.annotation.ChangedExam;
import com.hyt.it.ogt.ykcj.common.constant.Constants;
import com.hyt.it.ogt.ykcj.common.core.domain.model.LoginUser;
import com.hyt.it.ogt.ykcj.common.utils.ServletUtils;
import com.hyt.it.ogt.ykcj.common.utils.StringUtils;
import com.hyt.it.ogt.ykcj.common.utils.spring.SpringUtils;
import com.hyt.it.ogt.ykcj.framework.manager.AsyncManager;
import com.hyt.it.ogt.ykcj.framework.manager.factory.AsyncFactory;
import com.hyt.it.ogt.ykcj.framework.web.service.TokenService;

/**
 * @Author chentb
 * @Date 2021/8/10 17:14
 */

@Aspect
@Component
public class ChangedExamAspect {
    private static final Logger log = LoggerFactory.getLogger(ChangedExamAspect.class);

    // 配置织入点
    @Pointcut("@annotation(com.hyt.it.ogt.ykcj.common.annotation.ChangedExam)")
    public void changedExamPointCut()
    {
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "changedExamPointCut()")
    public void doAfterReturning(JoinPoint joinPoint)
    {
        handleChangedExam(joinPoint, null);
    }

    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e 异常
     */
    @AfterThrowing(value = "changedExamPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e)
    {
        handleChangedExam(joinPoint, e);
    }

    protected void handleChangedExam(final JoinPoint joinPoint, final Exception e)
    {
        try
        {
            // 获得注解
            ChangedExam controllerLog = getAnnotationLog(joinPoint);
            if (controllerLog == null)
            {
                return;
            }

            // 获取当前的用户
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

            // *========更新的考试信息=========*//
            String examId = Constants.changedExamId.get();
            if (StringUtils.isNotEmpty(examId)){
                Constants.changedExamId.remove();

                // 保存数据库
                AsyncManager.me().execute(AsyncFactory.recordChangedExam(examId));
            }

        }
        catch (Exception exp)
        {
            // 记录本地异常日志
            log.error("==前置通知异常==");
            log.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }


    /**
     * 是否存在注解，如果存在就获取
     */
    private ChangedExam getAnnotationLog(JoinPoint joinPoint) throws Exception
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(ChangedExam.class);
        }
        return null;
    }

}
